<?php
/**
 * Easthv Zend Framework Project
 *
 *
 * LICENSE:
 *
 * @category   Zend
 * @package    Zend_Easthv
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
*/

/**
 * Validation class
 *
 * @category   Zend
 * @package    Zend_Easthv
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
 * @deprecated
 */
class Zend_Easthv_Validate
{
    /** 
     * @staticvar array   Validation message
     */ 
    protected static $_message = array();

    /** 
     * @staticvar array   Multi Validation messages
     */ 
    protected static $_messages = array();

    /** 
     * @staticvar array   Http response code
     */ 
    protected static $_code = 400;
    
    /** 
     * @staticvar array   Validation rules mapping
     */ 
    protected static $_ruleMapping = array(
        'GreaterThan' => 1,
        'LessThan' => 1,
        'InArray' => 1,
        'Regex' => 1,
        'StringLength' => 2,
        'Between' => 2,
    );
    
    /**
     * Get message
     *
     * @access public
     *
     * @return array message 
     */
    public static function getMessage()
    {
        return self::$_message;
    }
    
    /**
     * Get messages
     *
     * @access public
     *
     * @return array messages
     */
    public static function getMessages()
    {
        return self::$_messages;
    }
    
    /**
     * Get code
     *
     * @access public
     *
     * @return int code
     */
    public static function getCode()
    {
        return self::$_code;
    }

    /**
     * show valid messages
     *
     *
     * @access public
     *
     * @return string   valid messages
     */
    public static function cleanMessages($valid)
    {
        $res = array();

        foreach($valid as $model) {

            if (!$model) {

                continue;
            
            }

            $keys = array_keys($model);

            //multi element valid
            if (is_int($keys[0])) {

                foreach($model as $num => $_model) {
                
                    foreach($_model as $field => $list) {

                        foreach($list as $line) {

                            foreach($line as $rule => $msg) {


                                if ($msg != "'' is an empty string") {

                                    $order = array(
                                        1 => '1st',
                                        2 => '2nd',
                                        3 => '3rd',
                                    );
                                    $order = isset($order[$num + 1]) ? $order[$num + 1] : $num + 1 . 'th';
                                    $res[$field][] = "The $order one : " . str_replace("''","'$field'",$msg);

                                }


                            }


                        }

                    }            

                }


            } else {

                foreach($model as $field => $list) {

                    foreach($list as $line) {

                        if (is_array($line)) {

                            foreach($line as $rule => $msg) {


                                if ($msg != "'' is an empty string") {

                                    $res[$field][] = str_replace("''","'$field'",$msg);

                                }


                            }


                        } else {

                            if ($line != "'' is an empty string") {

                                $res[$field][] = str_replace("''","'$field'",$line);

                            }

                        }



                    }

                }        
            } 
        }
        //valid array end


        //delete repeat messages
        foreach($res as $key => $_res) {

            $res[$key] = array_unique($_res);
        
        }

        return $res;
    }

    /**
     * Validation for multi rules
     * 
     * @param string value
     * @param array $inputs
     *
     * @access public
     *
     * @return boolean  if validation passed return true, failed return false
     */
    public static function validates($value, $inputs = null)
    {

        self::$_messages = array();

        $res = true;
        foreach($inputs as $key => $input){
            $input['input'] = $value;

            $resone = self::validate($input);

            if ($resone === false) {
                self::$_messages[$key] = self::getMessage();
                $res = false;
            }
        }

        return $res;

    }

    /**
     * Validation by single rule
     * $input array format 
     * array(
     *      'rule' => validation rule
     *      'input' => value need to validate
     *      'message' => validation message
     *      'code' => http response code  //default is 400
     * )
     *
     * @param array $input
     * @access public
     *
     * @return boolean  if validation passed return true, failed return false
     */
    public static function validate($input)
    {
        self::$_message = array();

        if (!$input || !$input['rule']) {
            throw new Exception("Not set validation rule.");
        }

        $rule = $input['rule'];

        $validFunc = "valid$rule";

        if (method_exists('Zend_Easthv_Validate',$validFunc)) {
            return self::$validFunc($input);
        }

        if (isset(self::$_ruleMapping[$rule])) {
            $validFunc = "valid" . self::$_ruleMapping[$rule] . 'Params';
            return self::$validFunc($input);
        }

        $validClass = "Zend_Validate_$rule";

        $valid = new $validClass();

        $res = $valid->isValid($input['input']);

        self::_setAll($res, $input, $valid);
        return $res;
    }
    
    /**
     * Set validation rules / values / codes
     *
     * @access private
     * @param string $res
     * @param array $input
     * @param object $valid
     *
     * @return void
     */
    private static function _setAll($res, $input, $valid)
    {
        if ($res !== true) {
            self::$_message = $input['message'] ? array('userDefined' => $input['message']) : $valid->getMessages();
        }
        self::$_code = $input['code'] ? $input['code'] : 400;    
    }
    
    /**
     * 2 input params validation
     *
     * @access public
     * @param array $input
     *
     * @return boolean
     */
    public static function valid2Params($input)
    {
        $rule = $input['rule'];
        $validClass = "Zend_Validate_$rule";

        $valid = new $validClass($input['param'][0],$input['param'][1]);
        $res = $valid->isValid($input['input']);
        self::_setAll($res, $input, $valid);
        return $res;
    }
    
    /**
     * 
     * 1 input param validation
     *
     * @access public
     * @param array $input
     *
     * @return boolean
     */
    public static function valid1Params($input)
    {
        $rule = $input['rule'];
        $validClass = "Zend_Validate_$rule";

        $valid = new $validClass($input['param']);
        $res = $valid->isValid($input['input']);
        self::_setAll($res, $input, $valid);
        return $res;    
    }
    
    /**
     * 
     * Password validation
     *
     * @access public
     * @param array $input
     *
     * @return boolean
     */
    public static function validPassword($input = null)
    {
        $valid = new Zend_Validate_Password();
        $res = $valid->isValid($input['input']);
        self::_setAll($res, $input, $valid);
        return $res;
    }
    
    /**
     * 
     * Unique validation
     *
     * @access public
     * @param array $input
     *
     * @return boolean
     */
    public static function validUnique($input = null)
    {
        $valid = new Zend_Validate_Unique($input['param'], $input['exception']);
        $res = $valid->isValid($input['input']);
        self::_setAll($res, $input, $valid);
        return $res;
    }
}
